#include <stdio.h>
#include <serial.h>
#include <config.h>

int getchar(void)
{
	return serial_getc(CONFIG_CONSOLE_UART);
}

int putc(int c)
{
	serial_putc(CONFIG_CONSOLE_UART, c);
	return 0;
}

int puts(const char *s)
{
	serial_puts(CONFIG_CONSOLE_UART, s);
	return 0;
}

static void _puts_u8_hex(uint8_t dat)
{
	uint32_t tmp;

	tmp = (dat >> 4) & 0xf;
	putc((tmp > 9)? ('A' + tmp - 10): ('0' + tmp));
	tmp = dat & 0xf;
	putc((tmp > 9)? ('A' + tmp - 10): ('0' + tmp));
}

int puts_u8_hex(uint8_t dat)
{
	puts("0x");
	_puts_u8_hex(dat);
	return 0;
}

int puts_u16_hex(uint16_t dat)
{
	puts("0x");
	_puts_u8_hex(dat >> 8);
	_puts_u8_hex(dat);
	return 0;
}

int puts_u32_hex(uint32_t dat)
{
	puts("0x");
	_puts_u8_hex(dat >> 24);
	_puts_u8_hex(dat >> 16);
	_puts_u8_hex(dat >> 8);
	_puts_u8_hex(dat);
	return 0;
}

int vprintf(const char *fmt, va_list args)
{
    int res;
    char printbuffer[CONFIG_SYS_PBSIZE];

    /* For this to work, printbuffer must be larger than
     * anything we ever want to print.
     */
    res = vsnprintf(printbuffer, sizeof(printbuffer), fmt, args);

    /* Print the string */
    puts(printbuffer);
    return res;
}

int printf(const char *fmt, ...)
{
    va_list args;
	int res;

    va_start(args, fmt);
    res = vprintf(fmt, args);
    va_end(args);

    return res;
}

